package com.hoyue.classroom.service.impl;

import com.hoyue.classroom.bean.Course;
import com.hoyue.classroom.bean.UserCourse;
import com.hoyue.classroom.dao.CourseMapper;
import com.hoyue.classroom.dao.UserCourseMapper;
import com.hoyue.classroom.service.CourseService;
import com.hoyue.common.utils.DateUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 空白
 * @version 1.1
 * Create by 2019/08/17 00:40
 */
@Service
public class CourseServiceImpl implements CourseService {

    DateUtils dateUtils;

    @Resource
    CourseMapper courseMapper;

    @Resource
    UserCourseMapper userCourseMapper;

    @Override
    public List<Course> ListCourse(Integer id) {
        return userCourseMapper.listCourse(id);
    }

    @Override
    public Course getCourse(Integer id) {
        return courseMapper.getCourse(id);
    }

    @Override
    public Map<String,String> getCourseStr(Course course) {
        Map<String,String> map=new HashMap<String,String>();
        map.put("name",course.getName());
        map.put("describe",course.getDescribe());
        map.put("img",course.getImg());
        map.put("startTime",dateUtils.getDateToStr(course.getStartTime()));
        map.put("endTime",dateUtils.getDateToStr(course.getEndTime()));
        return map;
    }


    @Override
    public int insertCourse(String name, String describe, String startTime, String endTime, Integer userId, String userName) {
        // 获取当前时间
        Date time = dateUtils.getDateTime();
        // 将字符串时间转化成Date格式
        Date sTime =  dateUtils.getStrToDate(startTime);
        Date eTime = dateUtils.getStrToDate(endTime);
        Course course = new Course(name, describe, sTime, eTime, userId, userName, time, time);
        courseMapper.insertCourse(course);
        return course.getId();
    }

    @Override
    public int addCourse(UserCourse userCourse) {
        return userCourseMapper.addCourse(userCourse);
    }

    @Override
    public int updateCourse(Integer id, String name, String describe, String startTime, String endTime) {
        Date updateTime = dateUtils.getDateTime();
        Date sTime =  dateUtils.getStrToDate(startTime);
        Date eTime = dateUtils.getStrToDate(endTime);
        Course course = new Course(id, name, describe, sTime, eTime, updateTime);
        return courseMapper.updateCourse(course);
    }

    @Override
    public int delCourse(int courseId) {
        UserCourse userCourse = new UserCourse(null, courseId);
        // 将该课程拥有的学生退出课程
        userCourseMapper.movCourse(userCourse);
        // 删除该课程
        return courseMapper.delCourse(courseId);
    }

    @Override
    public int outCourse(int userId, int courseId) {
        UserCourse userCourse = new UserCourse(userId, courseId);
        // 将该课程拥有的学生退出课程
        return userCourseMapper.movCourse(userCourse);
    }

    @Override
    public List<Course> selectCourses(Course course) {
        return courseMapper.selectCourses(course);
    }


    // 当学生加入该课程之后的时候，考勤表添加学生的相关信息
    @Override
    public int addAttence(Integer studentid) {
        return courseMapper.insertAttence(studentid);
    }


}
